﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ledict.ConditionExtract.BLL;
using Ledict.ConditionExtract.Common;
using Ledict.ConditionExtract.Model;
namespace Ledict.ConditionExtract
{
	public partial class WB_EditWBKeyWords: Form
	{
		public WB_EditWBKeyWords()
		{
			InitializeComponent();
		}
        static WB_EditWBKeyWords _default;
        public static WB_EditWBKeyWords Default
        {
            get
            {
                if (_default == null || _default.IsDisposed)
                {
                    _default = new WB_EditWBKeyWords();
                }
                return _default;
            }
        }

        public string WB_ID ="";
        WordBankKeyWordsBLL wordbankkeywordsbll = new WordBankKeyWordsBLL();
        Express_WordBankRelBLL express_wordbankrelbll = new Express_WordBankRelBLL();
        DataTable Vocabdt ;

        private void WB_EditWBKeyWords_Load(object sender, EventArgs e)
        {
            GV_WB_AllWords.AutoGenerateColumns = false;
            GV_WB_AllWords.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            ShowGV_WB_AllWords();
        }
        public void ShowGV_WB_AllWords()
        {
            Vocabdt = null;
            dataPagerMain.ChangePageNumber(1);
        }
        /// <summary>
        /// 显示（下一页）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void dataPagerMain_PageNumberChanging(object sender, Controls.DataPagerPageIndexChangingEventArgs e)
        {

            var startRowIndex = e.StartRowIndex;
            var maximumRows = e.MaximumRows;
            var endRowIndex = e.StartRowIndex + e.MaximumRows;

            if (Vocabdt==null)
            Vocabdt = wordbankkeywordsbll.GetList_WB_IDAndWhere(WB_ID,"");


            if (Vocabdt.Rows.Count > 0)
            {
                e.TotalCount = Vocabdt.Rows.Count;

                DataTable newdt = new DataTable();
                newdt = Vocabdt.Clone();
                DataRow[] dr = Vocabdt.Select("showindex>" + startRowIndex + " and showindex<=" + endRowIndex);
                for (int i = 0; i < dr.Length; i++)
                {
                    newdt.ImportRow((DataRow)dr[i]);
                }

                GV_WB_AllWords.DataSource = newdt;
            }
            else
            {
                GV_WB_AllWords.DataSource = new DataTable();
            }
        }


        private void Add_Click(object sender, EventArgs e)
        {
            WB_AddKeyWords wb_addkeywords = new WB_AddKeyWords ();
            wb_addkeywords.WB_ID = WB_ID;
            wb_addkeywords.WBK_ID = "";
            wb_addkeywords. WBK_Word = "";
            wb_addkeywords.Show ();
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            string DeleteID = "";
            if (MessageBox.Show("确认要删除数据吗？", "此删除不可恢复", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    for (int i = 0; i <= GV_WB_AllWords.Rows.Count - 1; i++)
                    {
                        DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)GV_WB_AllWords.Rows[i].Cells["Column1"];
                        if (Convert.ToBoolean(check.Value) == true)
                        {
                            DeleteID = GV_WB_AllWords.Rows[i].Cells["WBK_ID"].Value.ToString();
                            if (DeleteID != "")
                            {
                                wordbankkeywordsbll.Delete(DeleteID);
                               // express_wordbankrelbll.Delete_Express_WordBankRel(DeleteID);

                            }

                        }
                    }
                    MessageBox.Show("删除成功!");
                }
                catch (Exception error)
                {
                    MessageBox.Show("删除失败!");
                }
                ShowGV_WB_AllWords();
            }
        }

        private void update_Click(object sender, EventArgs e)
        {
            WB_AddKeyWords wb_addkeywords =  WB_AddKeyWords.Default;
            for (int i = 0; i <= GV_WB_AllWords.Rows.Count - 1; i++)
            {
                DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)GV_WB_AllWords.Rows[i].Cells["Column1"];
                if (Convert.ToBoolean(check.Value) == true)
                {
                    wb_addkeywords.WBK_ID = GV_WB_AllWords.Rows[i].Cells["WBK_ID"].Value.ToString();
                    wb_addkeywords.WBK_Word = GV_WB_AllWords.Rows[i].Cells["WBK_Word"].Value.ToString();
                    wb_addkeywords.WB_ID = WB_ID;
                  
                    break;
                }
            }

            wb_addkeywords.Show();
        }

        private void SelectButton_Click(object sender, EventArgs e)
        {
            Vocabdt = wordbankkeywordsbll.GetList_WB_IDAndWhere(WB_ID, SelectText.Text .Trim ());
            dataPagerMain.ChangePageNumber(1);
        }


       

      
	}
}
